#include "registerviewmodel.h"

RegisterViewModel::RegisterViewModel(QObject *parent, TcpService* tcpclient)
    : QObject(parent)
    , tcpclient(tcpclient)
{
//    connect(tcpclient, &TcpService::messageReceived, this, &RegisterViewModel::respHandle);

}

bool RegisterViewModel::registerRequest(const QString &username, const QString &userid, const QString &password, const QString& head)
{
    qDebug() << "username: " << username << " userid: " << userid << " password: " << password;
    ProtocolRegisterReqPtr req = std::make_shared<ProtocolRegisterReq>();
    req->setUsername(username);
    req->setUserid(userid);
    req->setPassword(password);
    req->setHead(head);

    return tcpclient->sendData(req);
}

void RegisterViewModel::respHandle(const ProtocolPtr &resp)
{
    // 使用dynamic_pointer_cast进行安全转换
    ProtocolRegisterReqPtr registerPtr = std::dynamic_pointer_cast<ProtocolRegisterReq>(resp);

    if (registerPtr)
    {
        // 转换成功，指针有效
        qDebug() << "Regigter username: " << registerPtr->getUsername() << " userid: " << registerPtr->getUserid() << " password: " << registerPtr->getPassword();
    } else
    {
        // 转换失败
        qDebug() << "Failed to convert to ProtocolRegister";
    }
}



